


import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;

import javax.swing.JOptionPane;



public class Cascade {
	private List<OrderedCascade> cascade;
	
	public Cascade(){
		cascade = new LinkedList<OrderedCascade>();
	}
	public boolean add(OrderedCascade oc, boolean fromDeck){
		//Checks new cascade to see if we should just add the two together
		if(cascade.size() == 0){
			cascade.add(oc);
			return true;
		}
		if(fromDeck)
			return cascade.add(oc);
		
		if(cascade.get(cascade.size()-1).getTop().getValue() - 1 == oc.getBottom().getValue())
			if(cascade.get(cascade.size()-1).getTop().getSuite() % 2 != oc.getBottom().getSuite() % 2){
				cascade.get(cascade.size()-1).consolidate(oc);
				return true;
			}
		return false;
	}
	
	public OrderedCascade remove(int index){
		OrderedCascade oc;
		if(cascade.size()>=index && index>-1){
			oc = cascade.get(index).remove(0);
			if(cascade.get(index).size()==0)
				cascade.remove(index);
			return oc;
		}
		return null;
	}
	public int size(){
		int size = 0;
		for(OrderedCascade oc: cascade)
			for(int i=0; i<oc.size()-1; i++)
				size++;
		return size;
	}
	public ArrayList<Card> getCascade(){
		ArrayList<Card> temp = new ArrayList<Card>();
		for(int i=0; i<cascade.size(); i++)
			for(int j=0; j<cascade.get(i).size(); j++)
				temp.add(cascade.get(i).getOrderedCascadeCards(j));				
		return temp;
	}
	public OrderedCascade getOrderedCascade(int index){
		OrderedCascade temp;
		
		for(int i = 0; i<cascade.size(); i++){
			temp = cascade.get(i);
			for(int j=0; j<temp.size(); j++)
				if(temp.get(j).getIndex() == index){
					return temp;
				}
		}
		
		return null;
	}
	public OrderedCascade getBottom(){
		if(cascade.get(cascade.size()-1) != null)
			return cascade.get(cascade.size() - 1);
		return null;
	}
	public void removeBottom(){
		cascade.remove(cascade.size()-1);
	}
	public int getOrderedCascadeIndex(int index){
		OrderedCascade temp;
		
		for(int i = 0; i<cascade.size(); i++){
			temp = cascade.get(i);
			for(int j=0; j<temp.size(); j++)
				if(temp.get(j).getIndex() == index){
					return i;
				}
		}
		return -1;
	}
}
